package com.note.controller;

import com.alibaba.fastjson.JSON;
import com.note.entity.NoteContent;
import com.note.entity.UserNoteContent;
import com.note.service.NoteService;
import com.note.service.UserNoteContentService;
import com.note.service.UserService;
import com.note.servlet.Response;
import com.note.servlet.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by XQ on 2017/6/15.
 */
@RestController
public class NoteController {
    @Autowired
    UserService userService;
    @Autowired
    UserNoteContentService userNoteContentService;

    @Autowired
    NoteService noteService;

    /**
     * 添加一条行程
     * @param noteContent
     * @return
     */
    @RequestMapping(value = "/notes",method = RequestMethod.POST)
    public String addNote(@RequestBody NoteContent noteContent){

        NoteContent noteContent1 = new NoteContent();
        noteContent1.setTitle(noteContent.getTitle());
        noteContent1.setContent(noteContent.getContent());
        noteContent1.setTime(noteContent.getTime());
        noteService.insert(noteContent1);


        UserNoteContent userNoteContent = new UserNoteContent();
        userNoteContent.setUid(userService.findUserIdByUserAccount(noteContent.getUserAccount()).getUid());
        userNoteContent.setNcid(noteService.findNoteIdByTitle(noteContent.getTitle()).getNcid());
        userNoteContentService.insertRecord(userNoteContent);

        try{
            //      返回响应内容
            ResponseMessage responseMessage = new ResponseMessage();
            Response response  = null;
            response  = responseMessage.ResponseMessage(null,1,"上传成功！");
            return JSON.toJSONString(response);
        }catch (Exception e){
            System.out.println(e);
            //      返回响应内容
            ResponseMessage responseMessage = new ResponseMessage();
            Response response  = null;
            response  = responseMessage.ResponseMessage(null,0,"上传失败！");
            return JSON.toJSONString(response);
        }

    }

    /**
     * 查看用户的今日行程
     * @param userAccount
     * @return
     */
    @RequestMapping(value = "/notes/{userAccount}",method = RequestMethod.GET)
    public List<NoteContent> getNote(@PathVariable String userAccount){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return noteService.selectByTime(simpleDateFormat.format(new Date()),userAccount);

    }


}
